home *** CD-ROM | disk | FTP | other *** search
- /*
- EditTextFile.rexx v1.0 by Jeremy Friesner
-
- An ARexx script to cleanly modify text config
- lines. Any line beginning with sReplaceMe in
- the File fModifyMe will be replaced with the line sWithMe.
-
- If no such thing as sReplaceMe is found in the
- file, then sWithMe will be added to the end of
- the file.
-
- This script should never lose the original file:
- the first thing it does it rename the file to
- filename.bak and then reconstructs the modified
- version at the original file name by reading the
- .bak file.
-
- Any ^ signs in the sReplaceMe or sWithMe args will
- be turned into space characters. (Although sReplaceMe
- doesn't actually need these, because it's the last
- arg and thus spaces will be included in it anyway.
- Isn't Rexx argument passing fun?)
- */
-
- parse arg fModifyMe sReplaceMe sWithMe
-
- address COMMAND
-
- sReplaceMe = ChangeToSpaces(sReplaceMe)
- sWithMe = strip(ChangeToSpaces(sWithMe))
-
- fBackupFile = fModifyMe || ".bak"
- say "File to be modified = [" || fModifyMe || "]"
- say "Backup file = [" || fBackupFile || "]"
- say "String to replace = [" || sReplaceMe || "]"
- say "with = [" || sWithMe || "]"
-
- keylength = length(sReplaceMe)
-
- /* First, make sure rexxsupport.library is available. */
- call addlib("rexxsupport.library", 0, -30, 0)
-
- /* First, rename the original file to filename.bak */
-
- /* Delete the .bak file if it exists */
- call delete(fBackupFile)
- if (rename(fModifyMe, fBackupFile) == 0) then do
- say "Error: Couldn't rename " || fModifyMe || " to " || fBackupFile
- exit(30)
- end
-
- /* Default == haven't found the line we want to replace yet */
- BFoundOurLine = 0
-
- /* Open the backup file for reading */
- if (open('oldfile',fBackupFile,'R') == 0) then do
- say "Couldn't open backup file " || fBackupFile
- exit(31)
- end
-
- /* and the new file for writing */
- if (open('newfile',fModifyMe,'W') == 0) then do
- say "Couldn't open output file " || fModifyMe
- exit(32)
- end
-
- /* Scan the old file, writing the new one */
- do while (EOF('oldfile') == 0)
- nextline = readln('oldfile')
- sCheckPart = left(nextline,keylength)
-
- if ((BFoundOurLine == 0)&(sCheckPart = sReplaceMe)) then
- do
- /* don't write out this line... write out our substitute instead! */
- say "Found: [" || nextline || "] replacing with [" || sWithMe || "]"
- call writeln('newfile',sWithMe)
- BFoundOurLine = 1
- end
- else do
- call writeln('newfile',nextline)
- end
- end
-
- /* If we never found our line to replace, then just add our
- line at the end of the file. */
- if (BFoundOurLine == 0) then call writeln('newfile',sWithMe)
-
- call close('oldfile')
- call close('newfile')
-
- /* success! */
- say "File succesfully modified."
- exit(0)
-
-
- /* Changes all occurrences of the character '^' to spaces in the string.
- Needed to get around ARexx's lame argument parsing. I think.
- */
- ChangeToSpaces: procedure
- parse arg sOrig
-
- sNew = ""
- do while (length(sOrig) > 0)
- cChar = left(sOrig,1)
- if (cChar == "^") then cChar = " "
- sNew = sNew || cChar
- sOrig = right(sOrig,length(sOrig)-1)
- end
- return sNew
-